/**
 * Created by zxd on 2017/12/7.
 */


/**
 * Created by zxd on 2017/12/6.
 */

$(function(){
    loadDatas();
    reload()
});


function loadDatas(s){

    if(s=="search"){
        $("#page").val("");
    }

    $.ajax({
        type: "post",
        url: "../../php/vip/loadRandCodeList.php",
        beforeSend: function(){
            $("#load").css("display","block");
        },
        success: function(msg){
            $("#load").css("display","none");

            //表格数据
            var datas = msg.records;
            var html = "";
            if(datas.length == 0){
                html += "<tr><td colspan='8' align='center'>没查询到符合条件的数据~</td></tr>";
            }else{
                for(var i = 0; i<datas.length; i++){
                    html += "<tr>";
                    html += "<td>"+datas[i].code+"</td>";
                    html += "<td>"+datas[i].batch+"</td>";//昵称
                    html += "<td>"+datas[i].userid+"</td>";//昵称
                    html += "<td>"+datas[i].createtime+"</td>";//变动后金币
                    html += "<td>"+datas[i].recvtime+"</td>";//变动后金币
                    var isuse;
                    if(datas[i].isuse==0){
                        isuse="未使用";
                    }else if(datas[i].isuse==1){
                        isuse="已使用";
                    }
                    html += "<td>"+isuse+"</td>";//是否使用
                    var isrecv;
                    if(datas[i].isrecv==0){
                        isrecv="未接收";
                    }else if(datas[i].isrecv==1){
                        isrecv="已接收";
                    }
                    html += "<td>"+isrecv+"</td>";//是否接收
                    html += "<td>"+datas[i].gold+"</td>";//变动前金币
                    html += "</tr>";
                }
            }
            $("#databody").html(html);
        },error:function(msg){

        }
    });
}


function getCode(){
    var batch=$("#batch").val();
    if(batch==""){
        var msg="请输入批次";
        $("#message").val(msg);
        $("#tishi").modal("show");
        return false;
    }
    $.ajax({
        type: "post",
        data:{"batch":batch},
        url: "../../php/vip/loadUnUseRandCode.php",
        beforeSend: function(){
        },
        success: function(msg){

            var datas = msg.path;
            var code=datas[0].code;

            if(code==1){
                var url=datas[0].path;
                //创建下载链接
                $("#download").attr("href",url);
                var myDate = new Date();
                var time=myDate.getTime();
                //创建下载资源名称
                $("#download").attr("download",time);
                //模拟点击事件
				document.getElementById('download').click();
            } else {
                var content="分配失败！";
                $("#message").html(content);
                $("#tishi").modal("show");
            }


        },error:function(msg){
           console.log("数据错误");
        }
    });
}


function reload(){
    var url=$("#download").attr("href");

    if(url==""){
        $("#download").css("display","none");
    }else{
        window.location.reload();
    }

}
